package com.example.myapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

class MyDatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG ="MyDatabaseHelper" ;

    private  Context context;
    private  static final int VERSION  = 1;
    private  static final String DB_NAME = "my_state.db";
    private static final  String TB_NAME = "tb_states";
    private static final String COLUMN_ID ="id";
    private static final String COLUMN_WEIGHT ="weight";
    private static final String COLUMN_AGE ="age";
    private static final String COLUMN_HEIGHT ="height";
    private static final String COLUMN_SEX ="sex";
    private static final String COLUMN_TYPE ="type";


    public MyDatabaseHelper(@Nullable Context context) {
        super(context, DB_NAME, null,VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE "+TB_NAME+
                " (" +COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_AGE + " INTEGER, " +
                COLUMN_HEIGHT + " INTEGER, " +
                COLUMN_WEIGHT + " INTEGER, " +
                COLUMN_SEX + " TEXT, " +
                COLUMN_TYPE + " TEXT);";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " +TB_NAME);
        onCreate(db);
    }

    //在数据库中添加数据
    void addState(int age, int height, int weight, String sex, String type){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

        cv.put(COLUMN_AGE,age);
        cv.put(COLUMN_HEIGHT,height);
        cv.put(COLUMN_WEIGHT,weight);
        cv.put(COLUMN_SEX,sex);
        cv.put(COLUMN_TYPE,type);
        long result = db.insert(TB_NAME,null,cv);
        if (result==-1){
            Toast.makeText(context,"添加失败",Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(context,"添加成功",Toast.LENGTH_SHORT).show();
        }
        Log.i(TAG,"add:ret="+result );
    }
    //在数据库中读取所有数据
    Cursor readAllData(){
        String sql = "SELECT * FROM "+TB_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        if(db!=null){
            cursor = db.rawQuery(sql,null);
        }
        return cursor;

    }
    //在数据库中更新数据
    void updateData(String row_id,String age,String height,String weight,String sex,String type){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put(COLUMN_AGE,age);
        cv.put(COLUMN_HEIGHT,height);
        cv.put(COLUMN_WEIGHT,weight);
        cv.put(COLUMN_SEX,sex);
        cv.put(COLUMN_TYPE,type);

        long result = db.update(TB_NAME, cv,"id=?",new String[]{row_id});
        if (result==-1){
            Toast.makeText(context,"更新失败",Toast.LENGTH_SHORT).show();
        }
        else
            Toast.makeText(context,"更新成功",Toast.LENGTH_SHORT).show();

    }
    //删除一行
    void deleteOneRow(String row_id){
        SQLiteDatabase db = this.getWritableDatabase();
        long result = db.delete(TB_NAME,"id=?",new String[]{row_id});
        if (result ==-1){
            Toast.makeText(context,"failed",Toast.LENGTH_SHORT).show();

        }
        else {
            Toast.makeText(context,"删除成功",Toast.LENGTH_SHORT).show();


        }
    }
    //删除所有
    void deleteAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL("DELETE FROM "+TB_NAME);
    }


}
